package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.hosp.repository.HospitalRepository;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;


    //保存
    @Override
    public void save(Map<String, Object> map) {
        //将map集合转换为对象
        String mapstring = JSONObject.toJSONString(map);
        Hospital hospital = JSONObject.parseObject(mapstring, Hospital.class);

        //判断是否存在数据
        String hoscode = hospital.getHoscode();
        Hospital hospExist = hospitalRepository.getHospitalByHoscode(hoscode);


        //如果存在修改对象
        if(hospExist!=null){
            //设置医院状态 "状态 0：未上线 1：已上线"
            hospital.setStatus(hospExist.getStatus());
            hospital.setCreateTime(hospExist.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            //mongodb 修改和保存都是save
            hospitalRepository.save(hospital);
        }else {
            //不存在添加对象
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }


    }

    //查
    @Override
    public Hospital getByHoscode(String hoscode) {
        return hospitalRepository.getHospitalByHoscode(hoscode);
    }

    //带分页的查询页面
    @Override
    public Page<Hospital> getHospList(Integer pageNum, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //pageble
        Pageable pageable = PageRequest.of(pageNum-1,limit);
        //example
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        Example example = Example.of(hospital,ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true));
        //返回带分页的page对象
        Page<Hospital> pages = hospitalRepository.findAll(example,pageable);

        //获取查询的list集合，遍历进行医院等级的封装
        /*pages.get().forEach(item -> {
            this.setHospitalHosType(item);
        });*/
        pages.getContent().stream().forEach(item -> {
            this.setHospitalHosType(item);
        });

        return pages;
    }

    //获取查询的list集合，遍历进行医院等级的封装
    private Hospital setHospitalHosType(Hospital item) {
        //调用方法[根据两个属性获取医院等级名称]
        String hostypeString = dictFeignClient.getDictName("Hostype", item.getHostype());

        //查询省 市 地区
        String ProvinceString = dictFeignClient.getDictName(item.getProvinceCode());
        String CityString = dictFeignClient.getDictName(item.getCityCode());
        String DistrictString = dictFeignClient.getDictName(item.getDistrictCode());
        item.getParam().put("hostypeString", hostypeString);
        item.getParam().put("AllAddressString", ProvinceString+" "+CityString+" "+DistrictString);
        return item;
    }


    //更新医院上线状态
    @Override
    public void updateHospStatus(String id, Integer status) {
        Hospital hospitalById = hospitalRepository.getHospitalById(id);
        if (hospitalById!=null){
            hospitalById.setUpdateTime(new Date());
            hospitalById.setStatus(status);
            hospitalRepository.save(hospitalById);
        }
    }

    //设置医院详情
    @Override
    public Map<String,Object> showHospDetail(String id) {
        //return hospitalRepository.findById(id).get();
        Map<String,Object> map = new HashMap<>();
        Hospital hospital = this.setHospitalHosType(hospitalRepository.getHospitalById(id));
        map.put("hospital",hospital);
        //单独处理更直观
        map.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return map;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospitalByHoscode = hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospitalByHoscode!=null){
            return hospitalByHoscode.getHosname();
        }
        return null;
    }



    @Override
    public List<Hospital> findHospByHosName(String hosname) {

        return hospitalRepository.findByHosnameLike(hosname);
    }

    @Override
    public Map<String, Object> findHospDetail(String hoscode) {
        Hospital hospital = this.setHospitalHosType(this.getByHoscode(hoscode));

        Map<String, Object> map=  new HashMap<>();
        map.put("hospital",hospital);
        //预约规则
        map.put("bookingRule",hospital.getBookingRule());
        return map;
    }
}
